package com.tind.wyf.baselib.utils.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class RequestPermission {
    private static RequestPermission permission;
    //权限放置集合
    private static List<String> mPermissionList = new ArrayList<>();
    //勾选不在提示的权限
    private static List<String> mPermissionRationaleList = new ArrayList<>();

    public static RequestPermission getInstance() {
        if (null == permission) {
            permission = new RequestPermission();
        }
        return permission;
    }

    /**
     * 单个权限系统获取
     *
     * @param mClass
     * @param mPermission
     * @param requestCode
     * @param listener
     */
    public static void hasPermission(Object mClass, int requestCode, String mPermission, PermissionListener listener) {
        if (isContext(mClass, mPermission)) {
            //TODO 检测权限通过
            listener.permissionPass(requestCode);
        } else {
            //TODO 检测权限不通过
            requestPermission(mClass, requestCode, new String[]{mPermission});
        }

    }

    /**
     * 多个权限系统获取
     *
     * @param mClass
     * @param requestCode
     * @param permissions
     */
    public static void hasPermission(Object mClass, int requestCode, String[] permissions,
                                     PermissionListener listener) {
        for (int i = 0; i < permissions.length; i++) {
            if (!isContext(mClass, permissions[i])) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (null != mPermissionList && 0 != mPermissionList.size()) {
            String[] mPermissionArray = mPermissionList.toArray(new String[mPermissionList.size()]);
            requestPermission(mClass, requestCode, mPermissionArray);
            mPermissionList.clear();
            return;
        }
        if (null != listener)
            listener.permissionPass(requestCode);

    }

    /**
     * 用于用户拒绝了权限 继续弹出权限框
     * 用户勾选不再允许，调用自定义的dialog，去往设置界面
     *
     * @param mClass
     * @param requestCode
     * @param permissions
     * @param grantResults 权限返回是否通过
     * @param listener
     */
    public static void requestPermission(Object mClass, int requestCode, String[] permissions,
                                         int[] grantResults, PermissionListener listener) {

        for (int i = 0; i < grantResults.length; i++) {
            //将没有通过的权限取出来
            if (PackageManager.PERMISSION_DENIED == grantResults[i]) {
                if (!isRationale(mClass, permissions[i])) {
                    mPermissionRationaleList.add(permissions[i]);
                } else {
                    mPermissionList.add(permissions[i]);
                }
            }
        }

        if (null != mPermissionList && 0 != mPermissionList.size()) {//没有通过的权限继续申请权限
            String[] mPermission = mPermissionList.toArray(new String[mPermissionList.size()]);
            requestPermission(mClass, requestCode, mPermission);
            mPermissionList.clear();
            return;
        }
        if (null != mPermissionRationaleList && 0 != mPermissionRationaleList.size()) {
            if (null != listener) {
                listener.permissionRefuse(requestCode);
                mPermissionRationaleList.clear();
            }
            return;
        }

        if (null != listener)
            listener.permissionPass(requestCode);


    }


    /**
     * 调用上下文权限检测
     *
     * @param mClass
     * @param requestCode
     * @param toArray
     */
    private static void requestPermission(Object mClass, int requestCode, String[] toArray) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (mClass instanceof Activity) {
                ((Activity) mClass).requestPermissions(toArray, requestCode);
            } else if (mClass instanceof Fragment) {
                ((Fragment) mClass).requestPermissions(toArray, requestCode);
            }
        }
    }

    /**
     * 检测权限是否选取 禁止弹出
     *
     * @param mClass
     * @param mPermission
     * @return
     */
    private static boolean isRationale(Object mClass, String mPermission) {
        boolean b = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (mClass instanceof Activity) {
                b = ((Activity) mClass).shouldShowRequestPermissionRationale(mPermission);
            } else if (mClass instanceof Fragment) {
                b=((Fragment) mClass).shouldShowRequestPermissionRationale(mPermission);
            }
        }
        return b;
    }

    /**
     * 权限请求的发出地址
     *
     * @param mPermission
     * @return
     */
    private static boolean isContext(Object mClass, String mPermission) {
        boolean b;
        if (mClass instanceof Activity) {
            b = checkPermission((Activity) mClass, mPermission);
        } else {
            Fragment aClass = (Fragment) mClass;
            b = checkPermission(aClass.getContext(), mPermission);
        }
        return b;
    }

    /**
     * 检测是否具有权限
     *
     * @param
     * @param mPermission
     * @return
     */
    public static boolean checkPermission(Context context, String mPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(context, mPermission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        } else
            return true;
    }

    public static interface PermissionListener {
        /**
         * 权限请求通过
         *
         * @param requestcode
         */
        void permissionPass(int requestcode);

        /**
         * 权限请求拒绝 并勾选了不在提示
         *
         * @param requestcode
         */
        void permissionRefuse(int requestcode);
    }
}
